home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
webx
/
pws010.lzh
/
ServerCore.c
< prev
Wrap
C/C++ Source or Header
|
1998-09-11
|
7KB
|
320 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/dos.h>
#include <sys/stat.h>
#include <sys/xglob.h>
#include <network.h>
#include <socket.h>
int sock80, temp_sock = -1;
extern char *base_dir;
extern char *quit_str;
extern int v_option;
typedef struct {
char *content_type;
char *ext_type;
} TYPE_TABLE;
TYPE_TABLE type_table[]=
{
"text/html", "HTM",
"text/html", "HTML",
"text/plain", "TXT",
"text/plain", "DOC",
"image/gif", "GIF",
"image/jpeg", "JPG",
"image/jpeg", "JPEG",
NULL, NULL
};
/* â\âPâbâgé≡âIü[âvâôé╖éΘüiÅφÆôë≡Å£é╔Ägéñé╛é»üj */
int OpenSock (char *hostname, int port)
{
int sock;
struct hostent *h;
struct sockaddr_in addr;
/* â\âPâbâgé≡ì∞ɼé╖éΘ */
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
printf (" âGâëü[ : â\âPâbâgé¬ì∞ɼé┼é½é▄é╣é±é┼é╡é╜\n");
return (-1);
}
memset (&addr, 0, sizeof (addr)); /* 0 é┼ûäé▀éΘ */
addr.sin_family = AF_INET; /* INETâhâüâCâôé≡ÄwÆΦ */
addr.sin_port = htons (port); /* â|ü[âgö╘ìå */
/* âzâXâgû╝(www.xxx.co.jp) é≡ IP âAâhâîâX(int)é╔ò╧è╖ */
if ((h = gethostbyname (hostname)) == NULL) {
printf (" âGâëü[ : âhâüâCâôû╝é¬é▌é┬é⌐éΦé▄é╣é±\n");
return (-1);
}
addr.sin_addr.s_addr = *(long *) h->h_addr;
/* æèÄΦɵé╔É┌æ▒é╖éΘ */
if (v_option)
printf (" É┌æ▒Æå...\n");
if (connect (sock, (char *) &addr, sizeof (addr)) < 0) {
printf (" âGâëü[ : É┌æ▒é╔Ä╕ösé╡é▄é╡é╜\n");
return (-1);
}
return (sock);
}
/* ÄwÆΦé│éΩé╜â\âPâbâgé⌐éτâüâbâZü[âWé≡Ä≤é»ÄµéΘ */
int ReceiveMessage (int sock)
{
char temp_str[1024];
if (v_option)
printf ("Ä≤ÉMæ╥é┐...\n");
while (!socklen (sock, 0)); /* Ä≤ÉMæ╥é┐ */
do {
recvline (sock, temp_str, 1024); /* Ä≤ÉM */
if (v_option)
printf ("Ä≤ÉM > %s\n", temp_str);
} while (socklen (sock, 0) > 0);
return (0);
}
/* ÄwÆΦé│éΩé╜â\âPâbâgé╔âRâ}âôâhé≡æùéΘ */
int SendCommand (int sock, char *cmd)
{
if (v_option)
printf ("æùÉM > %s\n", cmd);
write_s (sock, cmd, strlen (cmd)); /* æùÉM */
while (!socklen (sock, 1)); /* æùÉMè«ù╣æ╥é┐ */
ReceiveMessage (sock);
return (0);
}
/* Passive ùpâ\âPâbâgé≡âIü[âvâôé╖éΘ */
int OpenPassiveSock (int port)
{
int sock;
struct sockaddr_in addr;
/* â\âPâbâgé≡ì∞ɼé╖éΘ */
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
printf (" âGâëü[ : â\âPâbâgé¬ì∞ɼé┼é½é▄é╣é±é┼é╡é╜\n");
return (-1);
}
memset (&addr, 0, sizeof (addr)); /* 0 é┼ûäé▀éΘ */
addr.sin_family = AF_INET; /* INETâhâüâCâôé≡ÄwÆΦ */
addr.sin_port = htons (port); /* â|ü[âgö╘ìå */
#if 0
addr.sin_addr.s_addr = INADDR_ANY;
#endif
addr.sin_addr.s_addr = htonl (0); /* INADDR_ANY:0 */
/* æèÄΦɵé╔É┌æ▒é╖éΘ */
if (bind (sock, (char *) &addr, sizeof (addr)) < 0) {
printf (" âGâëü[ : É┌æ▒é╔Ä╕ösé╡é▄é╡é╜\n");
return (-1);
}
if (listen (sock, 1) < 0) {
printf (" âGâëü[ : listen é┼âGâëü[é¬ö¡É╢é╡é▄é╡é╜\n");
return (-1);
}
return (sock);
}
/* É┌æ▒é│éΩéΘé╠é≡æ╥é┬ */
int AcceptSock (int sock)
{
struct sockaddr_in from;
int temp_sock;
int len = sizeof (from);
if ((temp_sock = accept (sock, (char *) &from, &len)) < 0) {
printf (" âGâëü[ : accept é┼âGâëü[é¬ö¡É╢é╡é▄é╡é╜\n");
return (-1);
}
return (temp_sock);
}
void CloseSock (int sock)
{
if (sock > 0)
close_s (sock);
}
int ServerInit (void)
{
if (_get_version ()< 0) {
printf ("(h)inetd.x é¬ÅφÆôé╡é─éóé▄é╣é±\n");
return (-1);
}
if ((sock80 = OpenPassiveSock (80)) < 0)
return (-1);
return (0);
}
int ServerMain (void)
{
char temp_str[1024];
char method[64], url[1024], version[64];
FILE *fp;
char *content_type = "application/octet-stream"; /* ôΣé╠ègÆúÄqé╠ÅΩìçâRâîé╔é╚éΘ */
char *p = url;
char *ext = NULL;
TYPE_TABLE *t;
if (v_option)
printf ("É┌æ▒é≡æ╥é┴é─éóé▄é╖\n");
if ((temp_sock = AcceptSock (sock80)) < 0)
return (-1);
sockmode (temp_sock, SOCK_ASCII);
seteol (temp_sock, "\r\n");
if (v_option)
printf ("É┌æ▒é│éΩé▄é╡é╜\n");
while (recvline (temp_sock, temp_str, sizeof (temp_str)) < 0);
sscanf (temp_str, "%s %s %s", method, url, version);
if (v_option)
printf (" âwâbâ_ > %s", temp_str);
#if 0
while (recvline (temp_sock, temp_str, sizeof (temp_str)) > 2) {
if (v_option)
printf (" âwâbâ_ > %s", temp_str);
}
#endif
do {
if (recvline (temp_sock, temp_str, sizeof (temp_str)) < 0)
break;
if (v_option)
printf (" âwâbâ_ > %s", temp_str);
} while (*temp_str != '\n');
/* ÅIù╣ò╢ÄÜù±é╠â`âFâbâN */
sprintf (temp_str, "/%s", quit_str);
if (!strnicmp (url, temp_str, strlen (temp_str))) {
close_s (temp_sock);
temp_sock = -1;
return (-1); /* ÅφÆôë≡Å£ */
}
if ((!strcmp (method, "HEAD")) || (!strcmp (method, "GET"))) {
struct stat sb;
strcpy (temp_str, base_dir);
_dellastsep (temp_str);
strcat (temp_str, url);
printf("é▒é▒é▄é┼é═ùêé─éóéΘ\n");
if ((fp = fopen (temp_str, "rb")) != NULL) {
/* âìü[âJâïâtâ@âCâïé╠ègÆúÄqé⌐éτ content_type é≡ô╛éΘ */
while (*p) {
if (*p++ == '.')
ext = p;
}
if (ext) {
t = type_table;
do {
if (!stricmp (ext, t->ext_type)) {
content_type = t->content_type;
break;
}
} while ((++t)->ext_type != NULL);
}
stat (temp_str, &sb);
if (v_option)
printf (" %s é≡æùÉMé╡é▄é╖\n", temp_str);
strcpy (temp_str, "HTTP/1.0 200 OK\r\n");
write_s (temp_sock, temp_str, strlen (temp_str));
sprintf (temp_str, "Content-Type: %s\r\n", content_type);
write_s (temp_sock, temp_str, strlen (temp_str));
sprintf (temp_str, "Content-Length: %d\r\n", sb.st_size);
write_s (temp_sock, temp_str, strlen (temp_str));
strftime (temp_str, 96, "Last-Modified: %a, %d %b %Y %T GMT\r\n", gmtime (&sb.st_ctime));
write_s (temp_sock, temp_str, strlen (temp_str));
strcpy (temp_str, "\r\n"); /* âwâbâ_é╠ÅIù╣ */
write_s (temp_sock, temp_str, strlen (temp_str));
#if 0
/* é▒éΩé╢éßé╚é±é⌐ô«é⌐é± */
while (!feof (fp))
write_s (temp_sock, temp_str, fread (temp_str, sizeof (char), sizeof (temp_str), fp));
#endif
if (!strcmp (method, "GET")) {
int r1, r2 = 0;
sockmode (temp_sock, SOCK_BINARY);
for (;;) {
r1 = fread (temp_str, sizeof (char), sizeof (temp_str), fp);
if ((r2 + r1) > sb.st_size) {
r1 = sb.st_size - r2;
write_s (temp_sock, temp_str, r1);
break;
}
r2 += r1;
write_s (temp_sock, temp_str, r1);
if (feof (fp))
break;
}
}
fclose (fp);
} else {
strcpy (temp_str, "HTTP/1.0 404 Not Found\r\n");
write_s (temp_sock, temp_str, strlen (temp_str));
if (!strcmp (method, "GET")) {
strcpy (temp_str, "Content-Type: text/html\r\n");
write_s (temp_sock, temp_str, strlen (temp_str));
strcpy (temp_str, "\r\n"); /* âwâbâ_é╠ÅIù╣ */
write_s (temp_sock, temp_str, strlen (temp_str));
strcpy (temp_str,
"<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>"
"<BODY><H1>404 Not Found</H1>\r\n"
"</BODY></HTML>\r\n"
);
write_s (temp_sock, temp_str, strlen (temp_str));
} else {
strcpy (temp_str, "\r\n"); /* âwâbâ_é╠ÅIù╣ */
write_s (temp_sock, temp_str, strlen (temp_str));
}
}
} else {
/* HEAD/GET ê╚èOé╠Åêù¥ */
}
while (!socklen (temp_sock, 1)); /* æùÉMè«ù╣æ╥é┐ */
shutdown (temp_sock, 0);/* Ä≤ÉMé╡é╜âfü[â^é≡Ä≤é»Äµéτé╕üCé╖é╫é─öpèⁿé╖éΘ */
shutdown (temp_sock, 1);/* TCP FINé≡æùÉMé╡üCâfü[â^é╠æùÅoé≡éΓé▀éΘ */
//shutdown (temp_sock, 2); /* connection é≡ aborté╖éΘ */
close_s (temp_sock);
temp_sock = -1;
return (0);
}
int ServerTini (void)
{
if (sock80 > 0)
close_s (sock80);
if (temp_sock > 0)
close_s (temp_sock);
return (0);
}